﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;

namespace CRMBiz
{
    public static class AssessmentLastResultHandler
    {

        /// <summary>
        /// function for get assessment last result
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DataSet GetAssessmentLastResultList(int pageIndex, int pageSize)
        {
            DataSet ds;
            try
            {
                var sbSql = new StringBuilder("SELECT *,(select count(*) from crm_assessmentlastresult) as Total from crm_assessmentlastresult order by AssessmentLId");
                sbSql.Append(" LIMIT ").Append((pageIndex - 1) * pageSize).Append(", ").Append(pageSize);
                ds = DBhelper.ExecuteDataset(sbSql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        /// <summary>
        /// function for add assessment last result
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int AddAssessmentLastResult(string AssessmentLastResult)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@AssessmentLastResult", AssessmentLastResult),
                };

                DataSet ds = new DataSet();
                string sbSql = "Select * From crm_assessmentlastresult Where AssessmentLastResult = @AssessmentLastResult";
                ds = DBhelper.ExecuteDataset(sbSql, para);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "insert into crm_assessmentlastresult(AssessmentLastResult) values(@AssessmentLastResult)";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }


        /// <summary>
        /// function for edit assessment last result
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int EditAssessmentLastResult(int AssessmentLId, string AssessmentLastResult)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@AssessmentLId", AssessmentLId),
                    DBhelper.SetParameter("@AssessmentLastResult", AssessmentLastResult),
                };

                DataSet ds = new DataSet();
                string sbSql = "Select * From crm_assessmentlastresult Where AssessmentLId <> @AssessmentLId And  AssessmentLastResult = @AssessmentLastResult";
                ds = DBhelper.ExecuteDataset(sbSql, para);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "update crm_assessmentlastresult set AssessmentLastResult = @AssessmentLastResult where AssessmentLId = @AssessmentLId";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }


        /// <summary>
        /// function for delete assessment last result
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool DeleteAssessmentLastResult(int AssessmentLId)
        {
            bool flag = false;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@AssessmentLId", AssessmentLId),
                };

                string sbSql = "delete from crm_assessmentlastresult where AssessmentLId=@AssessmentLId";
                DBhelper.ExecuteNonQuery(sbSql, para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
